import com.entity.User;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TestCode {
    public static void main(String[] args) throws IOException {
        //需要添加的图片
        String imageQRUrl = "/Users/huoyajing/Desktop/image1.jpg";
        //保存图片的路径
        String result = "/Users/huoyajing/Desktop/savelovely.png";
        FileInputStream inputStream = new FileInputStream(imageQRUrl);
        BufferedImage imageQR = ImageIO.read(inputStream);
        String lovelyNick = "可爱熊宝宝";
        int fansCount = 10;
        String socre = "4.9";
        BufferedImage bufferedImage = graphicsGenerationlovely(imageQR, lovelyNick, fansCount, socre);
        int temp = result.lastIndexOf(".") + 1;
        ImageIO.write(bufferedImage, result.substring(temp), new File(result));
    }
    public static BufferedImage graphicsGenerationlovely(BufferedImage imageQR,  String lovelyNick, int fansCount, String lovelyScore) {
        //整体图的高度和宽度
        int lovelyImageWidth = 400;
        int lovelyImageHeight = 531;
        //整体图合成
        BufferedImage bufferedImage = new BufferedImage(lovelyImageWidth, lovelyImageHeight, BufferedImage.TYPE_INT_RGB);
        //设置图片的背景色
        Graphics2D main = bufferedImage.createGraphics();
        main.fillRect(0, 0, lovelyImageWidth, lovelyImageHeight);
        //宝宝名称整合
        Graphics2D textIntro = bufferedImage.createGraphics();
        //颜色
        textIntro.setColor(new Color(51, 51, 51));
        //设置字体
        Font introFont = new Font("黑体", Font.PLAIN, 30);
        textIntro.setFont(introFont);
        FontMetrics fm = textIntro.getFontMetrics(introFont);
        int nickWidth = fm.stringWidth(lovelyNick);
        //因为名称为不固定，所以设置据左位置
        int nickWidthX = (lovelyImageWidth - nickWidth) / 2 - 10;
        //字体失真处理
        textIntro.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        textIntro.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        textIntro.drawString(lovelyNick, nickWidthX, 50);
        //宝宝评价和粉丝个数
        Graphics2D textIntro1 = bufferedImage.createGraphics();
        textIntro1.setColor(new Color(153, 153, 153));
        Font intro1Font = new Font("微软雅黑", Font.PLAIN, 26);
        textIntro1.setFont(intro1Font);
        String fansAndScore = "评价:" + lovelyScore + "分 " + " | " + " 粉丝:" + fansCount;
        int infoWidth = fm.stringWidth(fansAndScore);
        int infoWidthX = (lovelyImageWidth - infoWidth) / 2;
        //失真处理
        textIntro1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        textIntro1.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        textIntro1.drawString(fansAndScore, infoWidthX, 100);
        //可爱宝宝图
        Graphics graphicsQR = bufferedImage.getGraphics();
        graphicsQR.drawImage(imageQR, 70, 150, 250, 250, null);
        //固定文字
        Graphics2D fixed = bufferedImage.createGraphics();
        fixed.setColor(new Color(51, 51, 51));
        Font fixedFont = new Font("黑体", Font.PLAIN, 26);
        fixed.setFont(fixedFont);
        //失真处理
        fixed.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        fixed.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        fixed.drawString("可爱点赞奥，哈哈哈", 70, 500);
        return bufferedImage;
    }
}
